//
//  NKRegistrUserViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/15.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKRegistrUserViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import <SVProgressHUD.h>
#import <ZYKeyboardUtil.h>
@interface NKRegistrUserViewController ()<UITextFieldDelegate>

@property(nonatomic, strong) ZYKeyboardUtil *keyboardUtil;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UIButton *sendCodeButton;

@property (weak, nonatomic) IBOutlet UIImageView *phoneImage;
@property (weak, nonatomic) IBOutlet UIImageView *codeImage;
@property (weak, nonatomic) IBOutlet UIImageView *passImage;


@end

@implementation NKRegistrUserViewController


#pragma mark - 点击事件
/**
 获取验证码
 */
- (IBAction)getAuthAction:(id)sender {
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        self.phoneImage.hidden = NO;
        if (!error) {
            
            self.codeImage.image = [UIImage imageNamed:@"batch_Lefts_10"];
            self.sendCodeButton.enabled = NO;
            [self.sendCodeButton setTitle:@"成功" forState:UIControlStateNormal];
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];
//            NSLog(@"获取验证码成功");
            
        } else {
            
            self.phoneImage.image = [UIImage imageNamed:@"batch_Lefts_8"];
            [SVProgressHUD showErrorWithStatus:error.userInfo[@"getVerificationCode"]];
            
        }
        
        
    }];
    

}


/**
 提交验证码
 */
- (IBAction)registerAction:(id)sender {
    
    if([self.phoneTextField.text isEqualToString:@""]){
        
        return;
        
    }
    
    //判断密码长度
    self.passImage.hidden = NO;
    if(self.passwordTextField.text.length < 6){
        self.passImage.image = [UIImage imageNamed:@"batch_Lefts_8"];
        [SVProgressHUD showErrorWithStatus:@"密码至少6位"];
        return;
    }else{
        self.passImage.image = [UIImage imageNamed:@"batch_Lefts_10"];
        
        [SMSSDK commitVerificationCode:self.codeTextField.text phoneNumber:self.phoneTextField.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
            self.codeImage.hidden = NO;
            if (!error) {
                
                self.codeImage.image = [UIImage imageNamed:@"batch_Lefts_10"];
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                
                
                [[self getMainNavigation] showOtherViewController:@"NKLoginUserViewController" title:@"登录"];
                
                
                //保存到Preference
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:self.phoneTextField.text forKey:[@"phone="stringByAppendingString:self.phoneTextField.text]];
                [userDefaults setObject:self.passwordTextField.text forKey:[@"password="stringByAppendingString:self.phoneTextField.text]];
                [userDefaults synchronize];
                
            }else
            {
                NSLog(@"%zd",self.passwordTextField.text.length);
                
                self.codeImage.image = [UIImage imageNamed:@"batch_Lefts_8"];
                [SVProgressHUD showErrorWithStatus:error.userInfo[@"commitVerificationCode"]];
                //            NSLog(@"错误信息：%@",error);
                
               
                
            }
            
            
        }];//block end
        
    }//else end
    
    
}





#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIColor *color = [UIColor grayColor];
    NSAttributedString *phoneAttr = [[NSAttributedString alloc]
                                 initWithString:@"输入手机号"
                                     attributes:@{NSForegroundColorAttributeName:color}];
    NSAttributedString *codeAttr = [[NSAttributedString alloc]
                                 initWithString:@"输入验证码"
                                 attributes:@{NSForegroundColorAttributeName:color}];
    NSAttributedString *passwordAttr = [[NSAttributedString alloc]
                                    initWithString:@"输入密码"
                                    attributes:@{NSForegroundColorAttributeName:color}];
    
    UIView *phoneLine=[[UIView alloc]initWithFrame:CGRectMake(0,self.phoneTextField.height-1, self.phoneTextField.width, 1)];
    phoneLine.backgroundColor=[UIColor lightGrayColor];
    
    UIView *codeLine=[[UIView alloc]initWithFrame:CGRectMake(0,self.codeTextField.height-1, self.codeTextField.width, 1)];
    codeLine.backgroundColor=[UIColor lightGrayColor];
    
    UIView *passwordLine=[[UIView alloc]initWithFrame:CGRectMake(0,self.passwordTextField.height-1, self.passwordTextField.width, 1)];
    passwordLine.backgroundColor=[UIColor lightGrayColor];
    
    [self.phoneTextField addSubview:phoneLine];
    [self.codeTextField addSubview:codeLine];
    [self.passwordTextField addSubview:passwordLine];
    
    self.phoneTextField.attributedPlaceholder = phoneAttr;
    self.codeTextField.attributedPlaceholder = codeAttr;
    self.passwordTextField.attributedPlaceholder = passwordAttr;
    
    
    
    self.keyboardUtil = [[ZYKeyboardUtil alloc] init];
    __weak NKRegistrUserViewController *weakSelf = self;
    [_keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
        [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.view, weakSelf.phoneTextField, weakSelf.codeTextField, weakSelf.passwordTextField, nil];
    }];
    
    [SMSSDK registerApp:@"190580fafd438" withSecret:@"750ed6f90b0b5195481f02ff0e2fc662"];
    
    self.phoneTextField.delegate = self;
    self.codeTextField.delegate = self;
    self.passwordTextField.delegate = self;
    
}

#pragma mark - delegate
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.phoneTextField resignFirstResponder];
    [self.codeTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.phoneTextField resignFirstResponder];
    [self.codeTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
